package com.sam.nosql;

import android.app.Instrumentation;
import android.test.InstrumentationTestCase;

import com.sam.data.DbDataUtil;
import com.sam.data.TimeUtils;
import com.sam.data.UserData;
import com.snappydb.DB;
import com.snappydb.DBFactory;

import junit.framework.Assert;

/**
 * &lt;br&gt;Created
 *
 * @author wangds
 * @see
 */
public class SnappyDbTest extends InstrumentationTestCase{
    private MainActivity mMainActivity;
    private Instrumentation mInstrumentation;
    private DB snappyDB;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mMainActivity  = launchActivity("com.sam.nosql", MainActivity.class, null);
        snappyDB = DBFactory.open(mMainActivity, "snappyDb");
    }

    public void testList() throws Exception{
        DbDataUtil db = new DbDataUtil();
        TimeUtils.start();
        Object[] arraySave = db.list.toArray();
        snappyDB.put("list", arraySave);
        TimeUtils.end("SnappyDbTest.put");
        UserData[] array =  snappyDB.getObjectArray("list", UserData.class);
        TimeUtils.end("SnappyDbTest.getObjectArray");
        Assert.assertEquals(db.list.size(), array.length);

    }
    public void test() throws Exception{
        snappyDB.put("android:03", "Cupcake"); // adding 0 to maintain lexicographical order
        snappyDB.put("android:04", "Donut");
        snappyDB.put("android:05", "Eclair");
        snappyDB.put("android:08", "Froyo");
        snappyDB.put("android:09", "Gingerbread");
        snappyDB.put("android:11", "Honeycomb");
        snappyDB.put("android:14", "Ice Cream Sandwich");
        snappyDB.put("android:16", "Jelly Bean");
        snappyDB.put("android:19", "KitKat");

        String [] keys = snappyDB.findKeys("android");
        assert keys.length == 9;

        keys = snappyDB.findKeys("android:0");
        assert keys.length == 5;

        assert snappyDB.get(keys[0]).equals("Cupcake");
        assert snappyDB.get(keys[1]).equals("Donut");
        assert snappyDB.get(keys[2]).equals("Eclair");
        assert snappyDB.get(keys[3]).equals("Froyo");
        assert snappyDB.get(keys[4]).equals("Gingerbread");

        keys = snappyDB.findKeys("android:1");
        assert keys.length == 4;

        assert snappyDB.get(keys[0]).equals("Honeycomb");
        assert snappyDB.get(keys[1]).equals("Ice Cream Sandwich");
        assert snappyDB.get(keys[2]).equals("Jelly Bean");
        assert snappyDB.get(keys[3]).equals("KitKat");


        // By Range [from .. to]

        keys = snappyDB.findKeysBetween("android:08", "android:11");
        assertEquals(3, keys.length);
        assertEquals("android:08", keys[0]);
        assertEquals("android:09", keys[1]);
        assertEquals("android:11", keys[2]);

        keys = snappyDB.findKeysBetween("android:05", "android:10");
        assertEquals(3, keys.length);
        assertEquals("android:05", keys[0]);
        assertEquals("android:08", keys[1]);
        assertEquals("android:09", keys[2]);
        keys = snappyDB.findKeysBetween("android:07", "android:09");
        assertEquals(2, keys.length);
        assertEquals("android:08", keys[0]);
        assertEquals("android:09", keys[1]);
        keys = snappyDB.findKeysBetween("android:13", "android:99");
        assertEquals(3, keys.length);
        assertEquals("android:14", keys[0]);
        assertEquals("android:16", keys[1]);
        assertEquals("android:19", keys[2]);


//        With offset and limit

        //return all keys starting with "android" after the first 5
        keys = snappyDB.findKeys("android", 5);
        assertEquals(4, keys.length);
        assertEquals("android:11", keys[0]);
        assertEquals("android:14", keys[1]);
        assertEquals("android:16", keys[2]);
        assertEquals("android:19", keys[3]);

//return 3 first keys starting with "android"
        keys = snappyDB.findKeys("android", 0, 3);
        assertEquals(3, keys.length);
        assertEquals("android:03", keys[0]);
        assertEquals("android:04", keys[1]);
        assertEquals("android:05", keys[2]);

//return the fourth key starting with "android" (offset 3, limit 1)
        keys = snappyDB.findKeys("android", 3, 1);
        assertEquals(1, keys.length);
        assertEquals("android:08", keys[0]);

//return the two first keys between android:14 and android:99
        keys = snappyDB.findKeysBetween("android:14", "android:99", 0, 2);
        assertEquals(2, keys.length);
        assertEquals("android:14", keys[0]);
        assertEquals("android:16", keys[1]);

//return the third key (offset 2, limit 1) after android:10 before android:99
        keys = snappyDB.findKeysBetween("android:10", "android:99", 2, 1);
        assertEquals(1, keys.length);
        assertEquals("android:16", keys[0]);


//        Keys Count
//        Counting is quicker than extracting values (if you don't need them). Especially on very big collections.
        assertEquals(9, snappyDB.countKeys("android"));
        assertEquals(5, snappyDB.countKeys("android:0"));
//        By Range [from .. to]
        assertEquals(3, snappyDB.countKeysBetween("android:08", "android:11"));
        assertEquals(3, snappyDB.countKeysBetween("android:13", "android:99"));

    }

//    public void close() throws Exception{
//        snappyDB.close();
//        snappyDB.destroy();
//    }

}
